#!/bin/bash

src=~/py2

do_pre_install()
{
    echo -e "\033[1;33m start install python2 depend...\033[0m"
    sudo apt install -y build-essential gcc g++ make checkinstall libncursesw5-dev openssl libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev
}

do_download(){

    echo -e "\033[1;33m start download Python-2.7.18.tar.xz...\033[0m"
    if [ ! -d "${src}" ] ;then 
      mkdir ${src}
    fi
    cd ~/py2

    if [ ! -f "Python-2.7.18.tar.xz" ] ;then
    wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tar.xz
    fi  
    echo -e "\033[1;33m download done...\033[0m"
}

do_uncompress(){
    echo -e "\033[1;33m start  uncompress Python-2.7.18.tar.xz...\033[0m"
    cd ${src}
    if [ ! -d "${src}/Python-2.7.18" ] ;then
        tar -xf Python-2.7.18.tar.xz
    fi 
    echo -e "\033[1;33m uncompress done...\033[0m"
}

do_config_make_install()
{
   echo -e "\033[1;33m config  ...\033[0m"
   cd ${src}/Python-2.7.18
   ./configure --enable-shared --prefix=/usr 
   echo -e "\033[1;33m config done ...\033[0m"
   echo -e "\033[1;33m make  ...\033[0m"
   make -j6
   echo -e "\033[1;33m install  ...\033[0m"
   sudo make install 
   echo -e "\033[1;33m all done  ...\033[0m"
  # echo export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/lib64 >>  ~/.bashrc
   source ~/.bashrc
}

set -e
do_pre_install
do_download
do_uncompress
do_config_make_install
